<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="danger" size='small' @click="clearData" class="clearData">{{$t('清空')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="neighborList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55"></el-table-column>
      <el-table-column prop="ip" :label="$t('IPv6地址')" align='center' min-width="200">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.ip" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="mac" :label="$t('MAC地址')" align='center' min-width="200"></el-table-column>
      <el-table-column prop="dev" :label="$t('网口')" align='center' min-width="120"></el-table-column>
      <el-table-column prop="state" :label="$t('状态')" align='center' min-width="150"></el-table-column>
      <el-table-column prop="is_static" :label="$t('类型')" align='center' min-width="120"></el-table-column>
      <el-table-column prop="right" align='center' fixed="right" :label="$t('操作')" width="100">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row)" :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="record.page" :page-size="record.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination> -->

    <neighbor-dialog v-if="neighborDialogVisible" ref="refNeighborDialogVisible"></neighbor-dialog>
  </div>
</template>
<script>
import { getNeighborArray, cleanNeighbor, delNeighbor } from '@/api/network/mac/neighbor'
import NeighborDialog from './components/NeighborDialog'
// import 'view-design/dist/styles/iview.css'
export default {
  name: 'Neighbor',
  components: {
    NeighborDialog
  },
  data () {
    return {
      neighborDialogVisible: false,
      neighborList: [],
      record: {
        page: 1,
        limit: 20
      },
      // total: 0,
      flowSelectArray: []
    }
  },
  created () {
    this.listData()
  },
  methods: {
    // handleSizeChange (val) {
    //   // console.log(`每页 ${val} 条`)
    //   this.record.limit = val
    //   this.listData()
    // },
    // handleCurrentChange (val) {
    //   // console.log(`当前页: ${val}`)
    //   this.record.page = val
    //   this.listData()
    // },
    // 列表
    listData () {
      getNeighborArray().then(res => {
        if (res.status === 0) {
          // this.total = res.count
          this.neighborList = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.neighborDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refNeighborDialogVisible.init(data)
      })
    },
    // 多选按钮
    handleSelectionChange (data) {
      var flowSelectArray = []
      for (var item of data) {
        flowSelectArray.push({ ip: item.ip, dev: item.dev })
      }
      this.flowSelectArray = flowSelectArray
    },
    deleteData () {
      if (this.flowSelectArray && this.flowSelectArray.length > 0) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delNeighbor({ rule_map: JSON.stringify(this.flowSelectArray) }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.flowSelectArray = []
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    clearData () {
      this.$confirm(this.$t('您是否要清空所有数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        cleanNeighbor().then(res => {
          if (res.status === 0) {
            this.$message({
              message: this.$t('清空成功'),
              type: 'success'
            })
            this.listData()
          }
        })
      })
    }
  }
}
</script>
